<template>
	<view class="padding">
		<!-- <view>{{v}}</view> -->
		<view v-if="failOpenMsg" class="flex align-center bg-red light radius padding text-lg" v-html="failOpenMsg"></view>
		<button class="bg-blue" size="mini" @click="openFile()">打开</button>
	</view>
</template>

<script>
import { mapState, mapActions } from 'pinia';
import {
		useLoginUserStore
	} from '@/stores/login-user.js';
const dd = require('@/static/js/dingtalk.open.js');
export default {
	data() {
		return {
			opt: {
				pathUrl:''
			},
			param:'',
			authCode: null,
			failOpenMsg: '',
			order: '',
			v:"init"
		};
	},
	computed: {
		...mapState(useLoginUserStore, ['hasLogin'])
	},
	methods: {
		...mapMutations(useLoginUserStore, ['setToken','login']),
		openFile(){
			
			uni.navigateTo({
				url:"/pages/sso/wechat/wechat?fileId=92&busCode=PUR0206&isOuter=true&orgId=1&wechatHistoryId=218"
			})
		},
		loginByAuthCode(authCode) {
			this.v = "loginByAuthCode"
			let _that = this;
			this.om.request('omSso/loginByDD', {
					authCode: authCode,
					appKey: this.opt.appKey,
					appSecret: this.opt.appSecret
				})
				.then(res => {
					_that.order = JSON.stringify(res)
					_that.v = "authCode:" + authCode 
					if (res.succeed) {
						_that.setToken(res.value.token)
						_that.login(res.value.accountInfo);
						_that.openPath();
					} else {
						_that.failOpenMsg = "单点失败："+res.msg
					}
				},err => {
					_that.v = "loginByAuthCode:err"
					_that.order = JSON.stringify(err)
					_that.failOpenMsg = "单点失败："+ _that.order
				});
		},
		openPath() {
			this.v = "openPath"
			let _that = this;
			if (this.opt.fileId && this.opt.busCode) {//单点表单显示页
				this.om.getFormInfo(this.opt.fileId, this.opt.busCode, true, function(msg){
					if(msg.includes("error.data.notExist")){
						_that.failOpenMsg = "打开失败，表单不存在";
					}else{
						_that.failOpenMsg = "表单打开失败：" + msg;
					}
				});
			} else if (this.opt.pathUrl) {//单点表单页
				let url =  this.opt.pathUrl
				if(!this.opt.pathUrl.includes("title=") && this.opt.title){
					if(this.opt.pathUrl.includes("?")){
						url += "&title=" + this.opt.title
					}else{
						url += "?title=" + this.opt.title
					}
				}
				if(url.indexOf('/pages/main/main/main') >= 0){
					uni.switchTab({
						url: '/pages/main/main/main',
						fail: function() {
							_that.failOpenMsg = `首页打开失败`;
						}
					});
				}else{
					uni.redirectTo({
						url: url,
						fail: function() {
							_that.failOpenMsg = `打开失败，路径${_that.opt.pathUrl}无效`;
						}
					});
				}
			} else {//免登主页
				uni.switchTab({
					url: '/pages/main/main/main',
					fail: function() {
						_that.failOpenMsg = `首页打开失败`;
					}
				});
			}
		},
		copy(){
			this.v = "copy"
			let _that = this
			_that.om.toast('start');
			uni.setClipboardData({
				data: this.authCode,
				success: function () {
					_that.om.toast('success');
				}
			});
		}
	},
	onLoad: function(options) {
		this.v = "onLoad"
		uni.setNavigationBarTitle({
			title:options.title || "O&M"
		})
		this.opt = options;
		this.param = JSON.stringify(options)
		let _that = this;
		if (this.hasLogin) {
			this.v = "hasLogin"
			console.log("hasLogin = true")
			this.openPath();
		} else {
			this.v = "noLogin"
			if (dd.env.platform !== 'notInDingTalk') {
				this.v = "inDingTalk"
				//进行钉钉登录操作
				dd.ready(function() {
					// dd.ready参数为回调函数，在环境准备就绪时触发，jsapi的调用需要保证在该回调函数触发后调用，否则无效。
					dd.runtime.permission.requestAuthCode({
						corpId: options.corpId,
						onSuccess: function(result) {
							_that.v = "result"
							_that.authCode = result.code;
							_that.loginByAuthCode(result.code);
						},
						onFail: function(err) {
							_that.v = err
							_that.failOpenMsg = `打开失败：` + JSON.stringify(err);
						}
					});
				});
			} else {
				this.v = "notInDingTalk"
				this.failOpenMsg = "请从 <text class='text-cyan padding-sm text-xl text-bold'>钉钉工作台</text> 打开"
			}
		}
	}
};
</script>

<style></style>
